package com.bnq.control.desktop;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bnq.control.R;
import com.bnq.control.common.constant.CNTS;
import com.bnq.control.common.utils.DisplayUtils;
import com.bnq.control.common.utils.ToastUtils;
import com.bnq.control.desktop.entity.SDCardEntity;
import com.bnq.control.desktop.utils.DesktopPluginUtils;
import com.bnq.control.desktop.utils.SurfaceRecordUtils;
import com.bnq.control.desktop.view.AndroidBall;
import com.bnq.control.desktop.wrapper.RemoteDesktopListenerWrapper;
import com.oray.sunlogin.manager.DesktopPluginManager;
import com.oray.sunlogin.manager.encode.gles.GlUtil;
import com.oray.sunlogin.manager.encode.listener.YUVRenderListener;
import com.oray.sunlogin.manager.opengl.GlSurfaceMaskLayer;
import com.oray.sunlogin.manager.opengl.MediaCodecUtils;
import com.oray.sunlogin.manager.widget.RemoteDesktopSurfaceView;
import com.oray.sunlogin.plugin.JavaPlugin;


import java.io.File;

/**
 * Created by zou on 2016/11/9.
 */

public class RemoteAndroidActivity extends AppCompatActivity implements JavaPlugin.IConnectorListener {

    private static final String TAG = RemoteAndroidActivity.class.getSimpleName();
    private static final String HOME = "F1";             // HOME
    private static final String BACK = "F2";             // back
    private static final String MENU = "F3";             // Menu
    private static final String LOCK_SCREEN = "F5";      // 锁屏键
    private static final String VOICE_UP = "F6";         // 音量+
    private static final String VOICE_DOWN = "F7";       // 音量-
    private static final String DOWN_MENU = "F8";        // 下拉菜单
    private static final String UP_MENU = "F9";          // 上拉菜单

    public static final int MODE_OBSERVE = 0;
    public static final int MODE_CONTROL = 1;

    private int mMode = MODE_CONTROL;


    private AndroidBall androidBall;
    private View btn_android_menu_btn_end_remote;
    //控制模式菜单控件
    private View fl_android_menu_down, fl_android_menu_up, fl_android_menu_restart, fl_android_menu_shutdown,
            fl_android_menu_btn_voice_down, fl_android_menu_btn_voice_up, fl_android_menu_lock_screen,
            fl_android_menu_open_recording, fl_android_menu_screenshot, fl_android_menu_menu, fl_android_menu_home,
            fl_android_menu_back, fl_android_menu_switch_screen;
    //观看模式菜单控件
    private View fl_android_menu_open_recording_view, fl_android_menu_screenshot_view, fl_android_menu_switch_screen_view;

    private Button btn_android_menu_btn_end_remote_view;
    private ImageView iv_android_menu_recording, iv_android_menu_recording_view, iv_switch_screen, iv_switch_screen_view;
    private TextView tv_android_recording_control, tv_android_recording_view, tv_switch_screen, tv_switch_screen_view;
    private View android_control_pop, android_view_pop;
    private Activity mActivity;
    private ViewGroup rootView;

    private GlSurfaceMaskLayer mDesktopView;
    private RemoteDesktopSurfaceView mSurfaceView;
    private RemoteDesktopListener mListener;
    private MediaCodecUtils mediaCodecUtils;
    // 调用插件所需要的类
    private DesktopPluginManager mDesktopManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_desktop);

        mActivity = this;
        mListener = new RemoteDesktopListener();
        mDesktopManager = DesktopPluginManager.getInstance();
        initToolBar();
        initAndroidBall();
        initView();

        // 控制或观看模式
        mDesktopManager.setControlMode();

        mDesktopManager.resetNetworkDiagnosis();
        // 添加远程桌面的监听
        mDesktopManager.addListener(mListener);
        mDesktopManager.addConnectorListener(this);
    }

    private void initToolBar() {
        rootView = (ViewGroup) findViewById(R.id.fl_desktop);
        mDesktopView = (GlSurfaceMaskLayer) findViewById(R.id.remote_desktop_view);
        mSurfaceView = rootView.findViewById(R.id.surface);
        mDesktopView.setGlSurfaceView(mSurfaceView);
        mediaCodecUtils = new MediaCodecUtils();
        mSurfaceView.getSunloginRender().setOnSurfaceCreateListener(surface -> {
            mediaCodecUtils.setSurfaceView(mSurfaceView, surface, mDesktopManager, this::updateSizeChange);
        });

        mDesktopManager.setOnYUVRenderListener(new YUVRenderListener() {
            @Override
            public void onRenderYUV(int width, int height, byte[] y, byte[] u, byte[] v, boolean useNative) {
                mSurfaceView.getSunloginRender().setRenderType(GlUtil.RENDER_YUV);
                if (null != mediaCodecUtils) mediaCodecUtils.releaseMediaCodec();
                mSurfaceView.setYUVData(width, height, y, u, v, useNative);
            }

            @Override
            public void initMediaCodec(int width, int height, byte[] csd_0, byte[] csd_1, String mime) {
                mSurfaceView.getSunloginRender().setRenderType(GlUtil.RENDER_MEDIACODEC);
                mediaCodecUtils.initMediaCodec(width, height, mime);
            }

            @Override
            public void outputFormatChange(int width, int height) {
                mDesktopView.updateAlignmentSize(width, height);
            }

            @Override
            public void decodeFrameSize(int width, int height) {
                updateSizeChange(width, height);
            }

            @Override
            public void decodeFrame(int dataSize, byte[] data) {
                mediaCodecUtils.decodeFrameAsync(dataSize, data);
            }

            @Override
            public void useNativeRenderFilter(boolean useNative) {
                mSurfaceView.setUseNativeRender(useNative);
            }
        });
        mDesktopView.setDesktopPluginManager(mDesktopManager);
        mDesktopView.setAndroidMode(true);
        mDesktopView.setControl(true);
        Point displaySize = mDesktopManager.getDisplaySize();
        mDesktopView.updateDesktopRect(displaySize.x, displaySize.y);

        findViewById(R.id.remote_desktop_pop_viewgroup).setVisibility(View.GONE);
        findViewById(R.id.remote_desktop_mouse).setVisibility(View.GONE);
        findViewById(R.id.remote_desktop_circle).setVisibility(View.GONE);

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(() ->
                DisplayUtils.setFullScreenStatus(RemoteAndroidActivity.this));
    }

    /**
     * 更新窗口宽高
     *
     * @param width  宽度
     * @param height 高度
     */
    private void updateSizeChange(int width, int height) {
        if (null != mDesktopView && width > 0 && height > 0) {
            mDesktopView.updateDesktopRect(width, height);
        }
    }

    private void initAndroidBall() {
        androidBall = new AndroidBall(this, new AndroidBall.BallListener() {
            @Override
            public void onBallMove(float dx, float dy) {
                androidBall.setTranslationX(androidBall.getTranslationX() + dx);
                if ((dy < 0 && androidBall.getTranslationY() > 0) || (dy > 0 && androidBall.getTranslationY()
                        < DisplayUtils.getScreenHeight(mActivity) + 54)) {
                    androidBall.setTranslationY(androidBall.getTranslationY() + dy);
                }
            }

            @Override
            public void toSide() {
                if (androidBall.getTranslationY() < 0) {
                    androidBall.setTranslationY(0);
                } else if (androidBall.getTranslationY() > DisplayUtils.getScreenHeight(getApplication())) {
                    androidBall.setTranslationY(DisplayUtils.getScreenHeight(mActivity));
                }
                ObjectAnimator oa = ObjectAnimator.ofFloat(androidBall, "translationX",
                        getTranslationX());
                oa.setDuration(500);
                oa.start();
            }

            @Override
            public void onClick() {
                if (isControl()) {
                    if (android_control_pop.isShown()) {
                        android_control_pop.setVisibility(View.INVISIBLE);
                    } else {
                        android_control_pop.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (android_view_pop.isShown()) {
                        android_view_pop.setVisibility(View.INVISIBLE);
                    } else {
                        android_view_pop.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    /**
     * 从这里切换观看模式还是控制模式
     *
     * @return
     */
    public boolean isControl() {
        return MODE_CONTROL == mMode;
    }


    private int getTranslationX() {
        if (null != androidBall && androidBall.getTranslationX() < DisplayUtils.getScreenWidth(mActivity) >> 1) {
            return 0;
        } else {
            return DisplayUtils.getScreenWidth(mActivity) - DisplayUtils.dp2px(AndroidBall.WITH, mActivity);
        }
    }

    private void initView() {
        mDesktopView.setOnGetBitmapSizeListener((width, height) -> {
            if (width > height) {
                if (mActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    iv_switch_screen.setImageResource(R.drawable.vertical_screen);
                    iv_switch_screen_view.setImageResource(R.drawable.vertical_screen);
                    tv_switch_screen.setText(R.string.remote_android_screen_rotation);
                    tv_switch_screen_view.setText(R.string.remote_android_screen_rotation);
                }
            } else if (height > width) {
                if (mActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    iv_switch_screen.setImageResource(R.drawable.horizontal_screen);
                    iv_switch_screen_view.setImageResource(R.drawable.horizontal_screen);
                    tv_switch_screen.setText(R.string.remote_android_screen_rotation);
                    tv_switch_screen_view.setText(R.string.remote_android_screen_rotation);
                }
            }
        });

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(DisplayUtils.dp2px(AndroidBall.WITH, mActivity),
                DisplayUtils.dp2px(AndroidBall.HEIGHT, mActivity) * 2 + DisplayUtils.dp2px(2, mActivity));
        androidBall.setLayoutParams(lp);
        rootView.addView(androidBall);
        androidBall.setTranslationY(DisplayUtils.dp2px(200, mActivity));

        initViewPop();
        initControlPop();
    }

    private void initViewPop() {
        //TODO 观看模式
        android_view_pop = View.inflate(mActivity, R.layout.android_view_pop, null);
        fl_android_menu_open_recording_view = android_view_pop.findViewById(R.id.fl_android_menu_open_recording_view);
        fl_android_menu_screenshot_view = android_view_pop.findViewById(R.id.fl_android_menu_screenshot_view);
        fl_android_menu_switch_screen_view = android_view_pop.findViewById(R.id.fl_android_menu_switch_screen_view);
        btn_android_menu_btn_end_remote_view = (Button) android_view_pop.findViewById(R.id.btn_android_menu_btn_end_remote_view);
        iv_android_menu_recording_view = (ImageView) android_view_pop.findViewById(R.id.iv_android_menu_recording_view);
        tv_android_recording_view = (TextView) android_view_pop.findViewById(R.id.tv_android_recording_view);
        iv_switch_screen_view = (ImageView) android_view_pop.findViewById(R.id.iv_switch_screen_view);
        tv_switch_screen_view = (TextView) android_view_pop.findViewById(R.id.tv_switch_screen_view);

        setViewPopupListener();

        FrameLayout.LayoutParams lp_view_pop = new FrameLayout.LayoutParams(DisplayUtils.dp2px(276, mActivity),
                DisplayUtils.dp2px(202, mActivity));
        lp_view_pop.gravity = Gravity.CENTER;
        android_view_pop.setLayoutParams(lp_view_pop);
        rootView.addView(android_view_pop);
        android_view_pop.setVisibility(View.INVISIBLE);

    }

    private void initControlPop() {
        // TODO 控制模式
        android_control_pop = View.inflate(mActivity, R.layout.android_control_pop, null);
        fl_android_menu_down = android_control_pop.findViewById(R.id.fl_android_menu_down);
        fl_android_menu_up = android_control_pop.findViewById(R.id.fl_android_menu_up);
        fl_android_menu_restart = android_control_pop.findViewById(R.id.fl_android_menu_restart);
        fl_android_menu_shutdown = android_control_pop.findViewById(R.id.fl_android_menu_shutdown);
        fl_android_menu_btn_voice_down = android_control_pop.findViewById(R.id.fl_android_menu_btn_voice_down);
        fl_android_menu_btn_voice_up = android_control_pop.findViewById(R.id.fl_android_menu_btn_voice_up);
        fl_android_menu_lock_screen = android_control_pop.findViewById(R.id.fl_android_menu_lock_screen);

        fl_android_menu_open_recording = android_control_pop.findViewById(R.id.fl_android_menu_open_recording);
        fl_android_menu_screenshot = android_control_pop.findViewById(R.id.fl_android_menu_screenshot);
        fl_android_menu_menu = android_control_pop.findViewById(R.id.fl_android_menu_mune);
        fl_android_menu_home = android_control_pop.findViewById(R.id.fl_android_menu_home);
        fl_android_menu_back = android_control_pop.findViewById(R.id.fl_android_menu_back);
        fl_android_menu_switch_screen = android_control_pop.findViewById(R.id.fl_android_menu_switch_screen);
        android_control_pop.findViewById(R.id.fl_android_menu_keyboard).setEnabled(false);// 暂时不支持
        btn_android_menu_btn_end_remote = android_control_pop.findViewById(R.id.btn_android_menu_btn_end_remote);
        iv_android_menu_recording = (ImageView) android_control_pop.findViewById(R.id.iv_android_menu_recording);
        iv_switch_screen = (ImageView) android_control_pop.findViewById(R.id.iv_switch_screen);
        tv_android_recording_control = (TextView) android_control_pop.findViewById(R.id.tv_android_recording_control);
        tv_switch_screen = (TextView) android_control_pop.findViewById(R.id.tv_switch_screen);

        setControlPopupListener();

        FrameLayout.LayoutParams lp_view_pop = new FrameLayout.LayoutParams(DisplayUtils.dp2px(330, mActivity),
                DisplayUtils.dp2px(356, mActivity));
        lp_view_pop.gravity = Gravity.CENTER;
        android_control_pop.setLayoutParams(lp_view_pop);
        rootView.addView(android_control_pop);

        android_control_pop.setVisibility(View.INVISIBLE);
    }

    private void setViewPopupListener() {
        fl_android_menu_open_recording_view.setOnClickListener(v -> {
            if (SurfaceRecordUtils.hasPermission(v.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                android_view_pop.setVisibility(View.INVISIBLE);
                //TODO 录像
                if (mDesktopView.isSurfaceRecording()) {
                    stopSurfaceRecord();
                    iv_android_menu_recording_view.setImageDrawable(mActivity.getResources().
                            getDrawable(R.drawable.android_menu_open_recording));
                    tv_android_recording_view.setText(mActivity.getResources().getString(R.string.recording));
                    Toast.makeText(mActivity, "录影已停止", Toast.LENGTH_SHORT).show();
                } else {
                    startSurfaceRecord();
                    iv_android_menu_recording_view.setImageDrawable(mActivity.getResources().
                            getDrawable(R.drawable.android_menu_stop_recording));
                    tv_android_recording_view.setText(mActivity.getResources().getString(R.string.remotedesktop_tools_stop_screen_recorder));
                }
            } else {
                Toast.makeText(RemoteAndroidActivity.this, "没有外置SD卡读写权限", Toast.LENGTH_SHORT).show();
            }
        });
        fl_android_menu_screenshot_view.setOnClickListener(v -> {
            android_view_pop.setVisibility(View.INVISIBLE);
            screenshot();
            //TODO 截屏
        });
        fl_android_menu_switch_screen_view.setOnClickListener(v -> {
            android_view_pop.setVisibility(View.INVISIBLE);
            //TODO 切换屏幕
            mDesktopManager.rotateClientScreen();
            Toast.makeText(mActivity, R.string.screen_switching, Toast.LENGTH_SHORT).show();
        });
        btn_android_menu_btn_end_remote_view.setOnClickListener(v -> {
            //TODO 结束远控
            android_view_pop.setVisibility(View.INVISIBLE);
            onBackPressed();
        });
    }


    private void setControlPopupListener() {
        fl_android_menu_down.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(DOWN_MENU);       //TODO 下拉菜单
        });
        fl_android_menu_up.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(UP_MENU);         //TODO 上拉菜单
        });
        fl_android_menu_restart.setOnClickListener(v -> {
            //TODO 重启
            android_control_pop.setVisibility(View.INVISIBLE);
            mDesktopManager.restartRemote();
            ToastUtils.showSingleToast(R.string.restart_sended, mActivity);
        });
        fl_android_menu_shutdown.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            mDesktopManager.shutdownRemote();           //TODO 关机
            ToastUtils.showSingleToast(R.string.shutdown_sended, mActivity);

        });
        fl_android_menu_btn_voice_down.setOnClickListener(v -> {
            sendKeyMenu(VOICE_DOWN);         //TODO 音量-
        });
        fl_android_menu_btn_voice_up.setOnClickListener(v -> {
            sendKeyMenu(VOICE_UP);           //TODO 音量+
        });
        fl_android_menu_lock_screen.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(LOCK_SCREEN);         //TODO 锁屏键
        });
        fl_android_menu_switch_screen.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            mDesktopManager.rotateClientScreen();   //TODO 切换屏幕
            Toast.makeText(mActivity, R.string.screen_switching, Toast.LENGTH_SHORT).show();
        });

        fl_android_menu_open_recording.setOnClickListener(v -> {
            if (SurfaceRecordUtils.hasPermission(v.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                android_control_pop.setVisibility(View.INVISIBLE);
                //TODO 录像
                if (mDesktopView.isSurfaceRecording()) {
                    stopSurfaceRecord();
                    iv_android_menu_recording.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.android_menu_open_recording));
                    tv_android_recording_control.setText(mActivity.getResources().getString(R.string.recording));
                    Toast.makeText(mActivity, "录影已停止", Toast.LENGTH_SHORT).show();
                } else {
                    startSurfaceRecord();
                    iv_android_menu_recording.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.android_menu_stop_recording));
                    tv_android_recording_control.setText(mActivity.getResources().getString(R.string.remotedesktop_tools_stop_screen_recorder));
                }
            } else {
                Toast.makeText(RemoteAndroidActivity.this, "没有外置SD卡读写权限", Toast.LENGTH_SHORT).show();
            }
        });
        fl_android_menu_screenshot.setOnClickListener(v -> {

            android_control_pop.setVisibility(View.INVISIBLE);
            screenshot();         //TODO 截屏
        });
        fl_android_menu_menu.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(MENU);       //TODO 菜单
        });
        fl_android_menu_home.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(HOME);       //TODO home键
        });
        fl_android_menu_back.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            sendKeyMenu(BACK);       //TODO 返回键
        });
        btn_android_menu_btn_end_remote.setOnClickListener(v -> {
            android_control_pop.setVisibility(View.INVISIBLE);
            onBackPressed();         //TODO 结束远控
        });
    }

    private void sendKeyMenu(String key) {
        mDesktopManager.sendKeyUpDown(key, 0, true);
        mDesktopManager.sendKeyUpDown(key, 0, false);
    }

    private void startSurfaceRecord() {
        File file = new File(SurfaceRecordUtils.getFilePath(mActivity), SurfaceRecordUtils.getFileName());
        if (!file.exists()) file.getParentFile().mkdirs();
        mDesktopView.startRecorder(file.getAbsolutePath());
        Toast.makeText(mActivity, "正在开启录像", Toast.LENGTH_SHORT).show();
    }

    private void stopSurfaceRecord() {
        if (mDesktopView.isSurfaceRecording()) {
            mDesktopView.stopRecorder();
        }
    }

    // 截屏
    protected void screenshot() {
        SDCardEntity sdCardPath = DesktopPluginUtils.getSdCardPath();
        if (DesktopPluginUtils.isAvailable(sdCardPath.sdPath)) {
            if (null != mSurfaceView) mSurfaceView.screenShot();
            ToastUtils.showSingleToast(sdCardPath.isOutSD ? R.string.screenshot_place :
                    R.string.screenshot_place_internal_storage, mActivity);
        } else {
            ToastUtils.showSingleToast(R.string.screenshot_infos, mActivity);
        }
    }

    private class RemoteDesktopListener extends RemoteDesktopListenerWrapper {
        @Override
        public int onDisplayChanged(int width, int height, int bpp) {
            if (width > 0 && height > 0 && bpp > 0) {
                mDesktopView.updateDesktopRect(width, height);
                mDesktopView.requestLayout();
            }
            return 0;
        }

        @Override
        public void onSwitchHostEvent(int res, int errorCode) {

        }

        @Override
        public void onFirstFrame() {
            if (null != mDesktopView) mDesktopView.drawFirstFrame(true);
            if (null != mDesktopView) mDesktopView.requestLayout();
        }

        @Override
        public void onNewFrame(int i) {
            mDesktopView.requestLayout();
        }
    }

    @Override
    public void onStatusChanged(int nStatus, int nError) {
        if (CNTS.DISCONNECTED == nStatus) {
            Toast.makeText(mActivity, "远程连接已断开", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed() {
        if (android_control_pop != null && android_control_pop.isShown()) {
            android_control_pop.setVisibility(View.INVISIBLE);
        } else if (android_view_pop != null && android_view_pop.isShown()) {
            android_view_pop.setVisibility(View.INVISIBLE);
        } else {
            super.onBackPressed();
            stopConnected();
            if (null != mActivity) {
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }
    }

    private boolean isStopConnected = false;

    private void stopConnected(){
        isStopConnected = true;
        mDesktopView.removeOnGetBitmapSizeListener();
        mDesktopManager.removeAllListener();
        mDesktopManager.removeConnectorListener(this);
        mDesktopManager.cancelPlugin();
        stopSurfaceRecord();
    }

    @Override
    public void onResume() {
        super.onResume();
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mDesktopView.requestFocus();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onDestroy() {
        if(!isStopConnected){
            stopConnected();
        }
        super.onDestroy();
        if (null != mDesktopManager) mDesktopManager.shutDownSurface();
    }
}
